package com.kgcx.price.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgcx.common.bean.RequestData;
import com.kgcx.common.bean.ReturnData;
import com.kgcx.dbutils.utils.PageUtils;
import com.kgcx.price.domain.TPriceCaculationRule;
import com.kgcx.price.mapper.TPriceCaculationRuleMapper;
import com.kgcx.price.rabbitmq.BaseInfoProducer;
import com.kgcx.price.utils.SystemVariable;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * CaculationRule 报价规则类 Service层
 *
 * @author:zhoumeile
 * @date:2017/11/07
 */
@Service
public class TPriceCaculationRuleService {
    @Autowired
    private TPriceCaculationRuleMapper priceCaculationRuleMapper;

    @Autowired
    BaseInfoProducer baseInfoProducer;

    /**
     * getByCityIdCarType 根据城市Id,用车类型，取得报价信息列表
     *
     * @param tPriceCaculationRule 报价规则实体类
     * @return int
     */
    public TPriceCaculationRule getByCityIdCarType(TPriceCaculationRule tPriceCaculationRule) {
        return priceCaculationRuleMapper.getByCityIdCarType(tPriceCaculationRule);
    }

    /**
     * addPriceCaculationRule 新增报价规则信息
     *
     * @param tPriceCaculationRule 报价规则实体类
     * @return int
     */
    public ReturnData addPriceCaculationRule(TPriceCaculationRule tPriceCaculationRule) {

        if (StringUtils.isBlank(tPriceCaculationRule.getCityId().toString())) {
            return new ReturnData(SystemVariable.ERROR_RETURN_CODE, "城市id为空！");
        }

        if (StringUtils.isBlank(tPriceCaculationRule.getCarType().toString())) {
            return new ReturnData(SystemVariable.ERROR_RETURN_CODE, "用车类型为空！");
        }

        if (StringUtils.isBlank(tPriceCaculationRule.getUnitpricepermile().toString())) {
            return new ReturnData(SystemVariable.ERROR_RETURN_CODE, "unitpricepermile为空！");
        }

        if (StringUtils.isBlank(tPriceCaculationRule.getUnitpriceperminute().toString())) {
            return new ReturnData(SystemVariable.ERROR_RETURN_CODE, "unitpriceperminute为空！");
        }

        if (StringUtils.isBlank(tPriceCaculationRule.getFarAwayPrice().toString())) {
            return new ReturnData(SystemVariable.ERROR_RETURN_CODE, "farAwayPrice为空！");
        }

        if (StringUtils.isBlank(tPriceCaculationRule.getOutTownPrice().toString())) {
            return new ReturnData(SystemVariable.ERROR_RETURN_CODE, "outTownPrice为空！");
        }

        if (StringUtils.isBlank(tPriceCaculationRule.getNightPrice().toString())) {
            return new ReturnData(SystemVariable.ERROR_RETURN_CODE, "nightPrice为空！");
        }

        if (StringUtils.isBlank(tPriceCaculationRule.getStartFee().toString())) {
            return new ReturnData(SystemVariable.ERROR_RETURN_CODE, "startFee为空！");
        }

        if (StringUtils.isBlank(tPriceCaculationRule.getStartFee().toString())) {
            return new ReturnData(SystemVariable.ERROR_RETURN_CODE, "startFee为空！");
        }

        if (StringUtils.isBlank(tPriceCaculationRule.getNightPriceStartTime())) {
            return new ReturnData(SystemVariable.ERROR_RETURN_CODE, "nightPriceStartTime为空！");
        }

        if (StringUtils.isBlank(tPriceCaculationRule.getNightPriceEndTime())) {
            return new ReturnData(SystemVariable.ERROR_RETURN_CODE, "nightPriceEndTime为空！");
        }

        if (priceCaculationRuleMapper.getByCityIdCarType(tPriceCaculationRule) == null) {
//			TPriceCaculationRule tPriceCaculationRuleDB = tPriceCaculationRule;
//
//			/** 将前台传过来的单位转换为基本单位，公里转米，元转分 start */
//			tPriceCaculationRuleDB.setPackagePrice(tPriceCaculationRule.getPackagePrice() * 100);
//			tPriceCaculationRuleDB.setCommonTime(tPriceCaculationRuleDB.getCommonTime());
//			tPriceCaculationRuleDB.setCommonMileage(tPriceCaculationRule.getCommonMileage() * 1000);
//
//			tPriceCaculationRuleDB.setUnitpricepermile(tPriceCaculationRule.getUnitpricepermile() * 100 * 1000);
//			tPriceCaculationRuleDB.setUnitpriceperminute(tPriceCaculationRule.getUnitpriceperminute() * 60 * 1000);
//			tPriceCaculationRuleDB.setFarAwayPrice(tPriceCaculationRule.getFarAwayPrice() * 100 * 1000);
//			tPriceCaculationRuleDB.setOutTownPrice(tPriceCaculationRule.getOutTownPrice() * 100 * 1000);
//			tPriceCaculationRuleDB.setNightPrice(tPriceCaculationRule.getNightPrice() * 100 * 1000);
//			tPriceCaculationRuleDB.setStarFee(tPriceCaculationRule.getStartFee() * 100);
//			tPriceCaculationRuleDB.setRoadPrice(tPriceCaculationRule.getRoadPrice() * 100);
//			tPriceCaculationRuleDB.setParkPrice(tPriceCaculationRule.getParkPrice() * 100);
//			tPriceCaculationRuleDB.setOtherPrice(tPriceCaculationRule.getOtherPrice() * 100);

//			/** 将前台传过来的单位转换为基本单位，公里转米，元转分 end */

            if (priceCaculationRuleMapper.insertSelective(tPriceCaculationRule) > 0) {
                /** 发送mq消息到监管平台 */
                try {
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");

                    tPriceCaculationRule.setFarevalidon(new BigDecimal(simpleDateFormat.format(new Date())));
                    tPriceCaculationRule.setFarevalidoff(new BigDecimal(20501231000000L));
                    tPriceCaculationRule.setStarMile(0);
                    tPriceCaculationRule.setUpPrice(0);
                    tPriceCaculationRule.setUpPriceStartMile(0);
                    tPriceCaculationRule.setMorningpeaktimeon("0000");
                    tPriceCaculationRule.setMorningpeaktimeoff("0000");
                    tPriceCaculationRule.setEveningpeaktimeon("0000");
                    tPriceCaculationRule.setEveningpeaktimeoff("0000");
                    tPriceCaculationRule.setPeakpricestartmile(new BigDecimal(0));
                    tPriceCaculationRule.setLowSpeedPricePerMinute(0);
                    tPriceCaculationRule.setNightPricePerMinute(0);
                    tPriceCaculationRule.setOtherPeakTimeOff("0000");
                    tPriceCaculationRule.setOtherPeakTimeOn("0000");
                    tPriceCaculationRule.setPeakunitprice(new BigDecimal(0));
                    tPriceCaculationRule.setOtherPrice(new BigDecimal(0));

                    baseInfoProducer.addBaseInfoCaculationRule(tPriceCaculationRule);
                } catch (Exception e) {
                    e.printStackTrace();
                    return new ReturnData(SystemVariable.SUCCESS_RETURN_CODE, "新增报价规则成功");
                }

                return new ReturnData(SystemVariable.SUCCESS_RETURN_CODE, "新增报价规则成功");
            } else {
                return new ReturnData(SystemVariable.ERROR_RETURN_CODE, "新增报价规则失败");
            }
        } else {
            return new ReturnData(SystemVariable.ERROR_RETURN_CODE, "已有相同城市相同车辆类型报价规则，不允许新增！");
        }
    }

    /**
     * updateByCityIdCarType 根据城tPriceCaculationRule，更新报价信息
     *
     * @param tPriceCaculationRule 报价规则实体类
     * @return TPriceCaculationRule
     */
    @Transactional
    public ReturnData updateByPrimaryKeySelective(TPriceCaculationRule tPriceCaculationRule) {
        //if (priceCaculationRuleMapper.getByCityIdCarType(tPriceCaculationRule) == null) {
        if (priceCaculationRuleMapper.updateByPrimaryKeySelective(tPriceCaculationRule) > 0) {
            return new ReturnData(SystemVariable.SUCCESS_RETURN_CODE, "更新报价规则成功");
        } else {
            return new ReturnData(SystemVariable.ERROR_RETURN_CODE, "更新报价规则失败");
        }
        //} else {
        //    return new ReturnData(SystemVariable.ERROR_RETURN_CODE, "已有相同城市相同车辆类型报价规则，不允许新增！");
        //}
    }

    /**
     * getPriceCaculationRuleList 获取报价信息列表
     *
     * @return int
     */
    public ReturnData<PageInfo<TPriceCaculationRule>> getPriceCaculationRuleList(RequestData requestData) {
        PageUtils.page(requestData);
        PageHelper.orderBy(requestData.getOrderBy());
        List<TPriceCaculationRule> list = priceCaculationRuleMapper.getPriceCaculationRuleList();
        PageInfo<TPriceCaculationRule> info = new PageInfo<>(list);

        ReturnData<PageInfo<TPriceCaculationRule>> returnData = new ReturnData<>(info, info.getPages(), SystemVariable.SUCCESS_RETURN_CODE, "获取报价列表成功", info.getPageNum());
        returnData.setTotal((int) info.getTotal());
        return returnData;
    }

    /**
     * getPriceCaculationRuleList 获取报价信息列表
     *
     * @return int
     */
    public ReturnData<PageInfo<TPriceCaculationRule>> getPriceCaculationRuleListByCondition(RequestData requestData) {
        PageUtils.page(requestData);
        PageHelper.orderBy(requestData.getOrderBy());

        TPriceCaculationRule tPriceCaculationRule = new TPriceCaculationRule();

        if (StringUtils.isNotBlank(requestData.getString("carType"))) {
            tPriceCaculationRule.setCarType(Integer.parseInt(requestData.getString("carType")));
        }

        if (StringUtils.isNotBlank(requestData.getString("type"))) {
            tPriceCaculationRule.setType(Integer.parseInt(requestData.getString("type")));
        }

        if (StringUtils.isNotBlank(requestData.getString("cityId"))) {
            tPriceCaculationRule.setCityId(Integer.parseInt(requestData.getString("cityId")));
        }

        if (StringUtils.isNotBlank(requestData.getString("status"))) {
            tPriceCaculationRule.setStatus(Integer.parseInt(requestData.getString("status")));
        }
        List<TPriceCaculationRule> list = priceCaculationRuleMapper.getPriceCaculationRuleListByCondition(tPriceCaculationRule);
        PageInfo<TPriceCaculationRule> info = new PageInfo<>(list);

        ReturnData<PageInfo<TPriceCaculationRule>> returnData = new ReturnData<>(info, info.getPages(), SystemVariable.SUCCESS_RETURN_CODE, "获取报价列表成功", info.getPageNum());
        returnData.setTotal((int) info.getTotal());
        return returnData;
    }

    /**
     * 根据ID查询报价规则放到MQ里面给监管平台
     *
     * @param kid
     * @return
     */
    public TPriceCaculationRule getPriceCaculationRule(Long kid) {
        TPriceCaculationRule tPriceCaculationRule = priceCaculationRuleMapper.selectByPrimaryKey(kid);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        tPriceCaculationRule.setFarevalidon(new BigDecimal(simpleDateFormat.format(new Date())));
        tPriceCaculationRule.setFarevalidoff(new BigDecimal(20501231000000L));
        tPriceCaculationRule.setStarMile(0);
        tPriceCaculationRule.setUpPrice(0);
        tPriceCaculationRule.setUpPriceStartMile(0);
        tPriceCaculationRule.setMorningpeaktimeon("0000");
        tPriceCaculationRule.setMorningpeaktimeoff("0000");
        tPriceCaculationRule.setEveningpeaktimeon("0000");
        tPriceCaculationRule.setEveningpeaktimeoff("0000");
        tPriceCaculationRule.setPeakpricestartmile(new BigDecimal(0));
        tPriceCaculationRule.setLowSpeedPricePerMinute(0);
        tPriceCaculationRule.setNightPricePerMinute(0);
        tPriceCaculationRule.setOtherPeakTimeOff("0000");
        tPriceCaculationRule.setOtherPeakTimeOn("0000");
        tPriceCaculationRule.setPeakunitprice(new BigDecimal(0));
        tPriceCaculationRule.setOtherPrice(new BigDecimal(0));
        baseInfoProducer.addBaseInfoCaculationRule(tPriceCaculationRule);
        return tPriceCaculationRule;
    }
}
